package com.javatian.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatian.dao.CommentInfoDao;
import com.javatian.dao.GoodsInfoDao;
import com.javatian.entity.*;
import com.javatian.service.*;
import exception.CustomException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品信息对外提供的服务的实现类
 */
@Service("goodsInfoService")
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Resource
    private GoodsInfoDao goodsInfoDao;

    @Resource
    private TypeInfoService typeInfoService;

    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private WorkerInfoService workerInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private CommentInfoDao commentInfoDao;

    /**
     * 增加一条记录
     *
     */
    @Override
    public GoodsInfo add(GoodsInfo goodsInfo) {
        fileListToFileIds(goodsInfo);
        goodsInfoDao.insertSelective(goodsInfo);
        return goodsInfo;
    }

    /**
     * 删除一条记录
     *
     * @param goodsInfo
     */
    @Override
    public void delete(Long id) {
        goodsInfoDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改一条记录
     *
     */
    @Override
    public void update(GoodsInfo goodsInfo) {
        fileListToFileIds(goodsInfo);
        //点赞数+1
        if (goodsInfo.getHot() != null && goodsInfo.getName() == null) {
            GoodsInfo goods = findById(goodsInfo.getId());
            goods.setHot(goods.getHot() + goodsInfo.getHot());
            goodsInfoDao.updateByPrimaryKeySelective(goods);
        }else {
            goodsInfoDao.updateByPrimaryKeySelective(goodsInfo);
        }
    }

    /**
     * 根据主键查询一条记录
     *
     * @param id
     */
    @Override
    public GoodsInfo findById(Long id) {
        return goodsInfoDao.selectByPrimaryKey(id);
    }

    /**
     * 查询所有记录
     */
    @Override
    public List<GoodsInfo> findAll() {
        return goodsInfoDao.selectAll();
    }

    /**
     * 根据条件分页查询
     *
     * @param name     名称
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @param user 用户信息
     * @return 当前页的List列表
     */
    @Override
    public PageInfo<GoodsInfo> findPage(Integer pageNum, Integer pageSize, String name, Account user) {
        PageHelper.startPage(pageNum,pageSize);
        if (user==null){
            throw new CustomException("1001","用户未登录");
        }
        List<GoodsInfo> all;
        if (user.getLevel()==1){            //管理员可以看所有的商品，
            all = goodsInfoDao.findByNameAndUserId(name,null,null);
        }else {                            //非管理员只能看自己上传的商品，
            all = goodsInfoDao.findByNameAndUserId(name,user.getId(),user.getLevel());
        }
        //遍历每一项，设置所属类型名称，用户名
        for (GoodsInfo goodsInfo:all) {
            getRelInfo(goodsInfo);
        }

        return PageInfo.of(all);
    }

    /**
     * 设置所属类型名称，用户名
     */
    private void getRelInfo(GoodsInfo goodsInfo){
        if (goodsInfo == null){
            return;
        }
        TypeInfo typeInfo = typeInfoService.findById(goodsInfo.getTypeId());
        if (typeInfo!=null){
            goodsInfo.setTypeName(typeInfo.getName());
        }

        Account userInfo = getUserInfo(goodsInfo.getUserId(), goodsInfo.getLevel());
        if (userInfo!=null){
            goodsInfo.setUserName(userInfo.getName());
        }
    }

    /**
     * 根据用户ID和用户等级获取用户信息
     */
    private Account getUserInfo(Long userId,Integer level){
        Account account = new Account();
        if (level == 1){
            account = adminInfoService.findById(userId);
        }else if (level == 2){
            account = workerInfoService.findById(userId);
        }else if (level == 3){
            account = userInfoService.findById(userId);
        }
        return account;
    }

    /**
     * 文件列表转换成以逗号隔开的字符串
     */
    private void fileListToFileIds(GoodsInfo goodsInfo){
        List<Long> fileList = goodsInfo.getFileList();
        if (!CollectionUtil.isEmpty(fileList)){
            goodsInfo.setFileIds(fileList.toString());
        }
    }

    /**
     * 根据商品类型获取热门商品
     */

    public List<GoodsInfo> findByType(Integer typeId) {
       return goodsInfoDao.findByType((typeId));
    }


    /**
     * 推荐商品
     */
   public PageInfo<GoodsInfo> findRecommendGoodsInfos(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsInfo> recommendGoodsInfos = goodsInfoDao.findRecommendGoodsInfos();
        return PageInfo.of(recommendGoodsInfos);
    }

    /**
     * 查询用户购买过的所有商品
     */
    @Override
    public List<GoodsInfo> getOrderGoods(Long userId, Integer level) {
        List<GoodsInfo> list = goodsInfoDao.getOrderGoods(userId, level);
        for (GoodsInfo goodsInfo:list){
            List<CommentInfo> commentInfoList = commentInfoDao.findByGoodsIdAndUserId(goodsInfo.getId(), userId, level);
            if (CollUtil.isEmpty(commentInfoList)){
                goodsInfo.setCommentStatus("未评价");
            }else {
                goodsInfo.setCommentStatus("已评价");
            }
        }
        return list;
    }

}




















